package com.justgame.project.generator.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


public class ClassUtils {

    public static String[] classFieldNames(Class<?> clazz){
        return classFieldNameList(clazz).toArray(String[]::new);
    }

    public static List<String> classFieldNameList(Class<?> clazz){
        return getDeclaredFields(clazz).stream().map(Field::getName).collect(Collectors.toList());
    }

    public static boolean containsFieldByName(Class<?> clazz,String fieldName){
        return classFieldNameList(clazz).stream().anyMatch((fn)->fn.equals(fieldName));
    }

    private static List<Field> getDeclaredFields(Class<?> clazz) {
        List<Field> fields = new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()));
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)){
            fields.addAll(getDeclaredFields(clazz.getSuperclass()));
        }
        return fields;
    }
}
